package ae.pcfc.cldos.online.web.controller.tksadmin.company.home;

import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import ae.pcfc.cldos.online.common.delegate.CompanyInformationDelegate;
import ae.pcfc.cldos.online.common.exception.NoSuchCompanyException;
import ae.pcfc.cldos.online.common.vo.CompanyInformationVO;
import ae.pcfc.cldos.online.common.vo.selectors.SystemValuesVO;
import ae.pcfc.cldos.online.web.Constants;
import ae.pcfc.cldos.online.web.ViewPath;
import ae.pcfc.cldos.online.web.controller.base.BasePageController;
import ae.pcfc.cldos.online.web.model.CompanyInformationModel;

/**
 * Controller that handles all {@link Company}-related requests, i.e.: all
 * requests for /TKSADMIN HOME PAGE
 * 
 * @author aSNA
 */
@Controller
@RequestMapping(ViewPath.__TKSADMIN_HOME)
public class TksAdminHomeController extends BasePageController {
	
	@Autowired
	@Qualifier("companyInformationDelegate")
	private CompanyInformationDelegate companyInformationDelegate;
	
	
	List<CompanyInformationVO> companyList;
	/**
	 * @param locale
	 * @return
	 */
	@ModelAttribute(Constants.__MEDICAL_TYPE_MAP_MODEL_KEY)
	public Map<String, String> populateMedicalTypeList(Locale locale){
		
		Map<String, String> result = new HashMap<String, String>();
		
		List<SystemValuesVO> medicalTypes = systemValuesDelegate.getMedicalType();
		
		for(SystemValuesVO medicalType :medicalTypes ){
			if(locale.getLanguage().equals("ar")){
				result.put(medicalType.getSvCode(), medicalType.getSvDescArb());
			}else {
				result.put(medicalType.getSvCode(), medicalType.getSvDesc());
			}
		}
		
		return result;
	}
	
	/**
	 * @param request
	 * @param model
	 * @return
	 * @throws NoSuchCompanyException
	 */
	@ModelAttribute("companyList")
	public List<CompanyInformationVO> populateCompanyList(HttpServletRequest request,ModelMap model) throws NoSuchCompanyException{
		if(companyList==null || companyList.size()<=1){
			companyList = companyInformationDelegate.getAllActiveCompanies();
		}
		return companyList;
	}
	
	/**
	 * @param request
	 * @param companyInformationModel
	 * @return
	 * @throws NoSuchCompanyException
	 */
	@ModelAttribute("CompanyInformationInfo")
	public CompanyInformationModel populateCompanyInformationInfoModel(HttpServletRequest request,@ModelAttribute(value = Constants.__COMPANY_INFO_MODEL_KEY) CompanyInformationModel companyInformationModel) {
		String tksId = companyInformationModel.getSelectedCompany();
		if(tksId!=null){
			CompanyInformationVO selectedCompanyVO = null;
			for(CompanyInformationVO companyInformationVO :companyList){
				if(companyInformationVO.getTksId()!=null && companyInformationVO.getTksId().equals(tksId)){
					selectedCompanyVO = companyInformationVO;
					break;
				}
			}
			CompanyInformationVO compVO = new CompanyInformationVO();
			try{
				if(selectedCompanyVO!=null){
					compVO = companyInformationDelegate.retriveTksAdminCompanyInformation(selectedCompanyVO.getCompanyLicenseNo(), selectedCompanyVO.getTksClientId(),tksId);
				}
			}catch(NoSuchCompanyException ex){
				System.out.println(ex);
			}catch(Exception exp){
				System.out.println(exp);
			}
			mapper.map(compVO, companyInformationModel);
			companyInformationModel.setCompanyLicenseNo(selectedCompanyVO.getCompanyLicenseNo());
			companyInformationModel.setCompanyName(selectedCompanyVO.getCompanyName());
			companyInformationModel.setCompanyNameAr(selectedCompanyVO.getCompanyNameAr());
			companyInformationModel.setShowCompanyInfo("SUCCESS");
		}
		return companyInformationModel;	
	}

	
	/**
	 * @param model
	 */
	@ModelAttribute
	public void populateBreadCrumbs(ModelMap model) {
		breadCrumbsFactoryService.populateMainPageBreadCrumb(model);
	}
	
	/**
	 * 
	 */
	@RequestMapping(value = "/show.htm", method = RequestMethod.GET)
	public void show(){
		if(companyList==null || companyList.size()==0){
			companyList = companyInformationDelegate.getAllActiveCompanies();
		}
	}
	
	/**
	 * @param request
	 * @param status
	 * @param errors
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/show.htm")
	public void form(
			HttpServletRequest request) throws Exception {
			
		
	}

}
